package com.webex.ta.hydra.core.action;

import com.webex.ta.hydra.core.*;
import com.webex.ta.hydra.core.engine.HydraContext;
import com.webex.ta.hydra.core.engine.HydraContextService;

import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-6
 * Time: 12:09:21
 */
public class LoadVariablesFromProperties extends AbstractAction {
    private static ParamDescriptor[] desc = new ParamDescriptor[1];
    static {
        desc[0] = new ParamDescriptor("absolute or relative path to properties");
    }

    public void execute(TestResult result, String... args) throws Exception {
        HydraContext context = HydraContextService.getContext();
        FileInputStream fis = new FileInputStream(FileService.getInstance().getFile(args[0]));
        Properties props = new Properties();
        props.load(fis);

        String response = "";
        for (Map.Entry entry : props.entrySet()) {
            context.getVariables().put(entry.getKey().toString(), entry.getValue());
            response += entry.getKey().toString() + "=" + entry.getValue().toString() + "\n";
        }

        result.setResponse(response);
    }

    public ParamDescriptor[] getParamDescriptors(Command cmd) {
        return desc;
    }
}
